package com.SH.controller;

import com.SH.pojo.Shopping;
import com.SH.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class ShoppingController {

    @Autowired
    private ShoppingService shoppingService;

    @GetMapping("/addCar")
    public String addCar(@RequestParam("uid") Integer uid,@RequestParam("gid") Integer gid,@RequestParam("cid") Integer cid){
        shoppingService.addCar(uid,gid);
        return "redirect:/toShop?cid="+cid;
    }

    @GetMapping("/shoppingCar")
    public String shoppingCar(@RequestParam("uid") Integer uid, Model model){
        List<Shopping> shoppings = shoppingService.getByUid(uid);
        model.addAttribute("shoppings",shoppings);
        return "buyer/shoppingCar";
    }

    @GetMapping("/deleteGood")
    public String deleteGood(@RequestParam("id") Integer id,@RequestParam("uid") Integer uid){
        shoppingService.deleteGood(id);
        return "redirect:/shoppingCar?uid="+uid;
    }

    @GetMapping("/order")
    public String order(@RequestParam("id") Integer id,Model model){
        Shopping shopping = shoppingService.getById(id);
        model.addAttribute("shopping",shopping);
        return "buyer/order";
    }

    @GetMapping("/buy")
    public String buy(@RequestParam("id") Integer id,@RequestParam("uid") Integer uid){
        shoppingService.buyGoods(id);
        return "redirect:/shoppingCar?uid="+uid;
    }

    @GetMapping("/getBought")
    public String getBought(@RequestParam("uid") Integer uid,Model model){
        List<Shopping> bought = shoppingService.getBought(uid);
        model.addAttribute("bought",bought);
        return "buyer/bought";
    }

    @GetMapping("/buy2")
    public String buy2(@RequestParam("gid") Integer gid,@RequestParam("uid") Integer uid,Model model){
        shoppingService.addCar(uid,gid);
        List<Shopping> list = shoppingService.getByUIdAndGid(uid, gid);
        Shopping temp = list.get(0);
        Shopping shopping = shoppingService.getById(temp.getId());
        model.addAttribute("shopping",shopping);
        return "buyer/order";
    }
}
